//
//  Main.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2024/12/24.
//

import SwiftUI

struct Main: View {
    @EnvironmentObject var themeManager: ThemeManager
    @EnvironmentObject var pathManager: PathManager
    @EnvironmentObject var commonManager: CommonManager
    @EnvironmentObject var backupsManager: BackupsManager
    @EnvironmentObject var mainViewModel: MainViewModel
    @EnvironmentObject var appStorageManager: AppStorageManager
    
    init() {
        TabBarUtils.setCommonBar()
        EmojiPictureUtils.shared.delEmojiPicture()
    }
    
    var body: some View {
        NavigationStack(path: $pathManager.path) {
            TabView {
                HomeView()
                    .tabItem {
                        Image(.mainIconHome)
                            .resizable()
                            .renderingMode(.template)
                            .scaledToFit()
                            .foregroundColor(themeManager.themeColor)
                            .frame(width: 10, height: 10)
                        Text("首页")
                        
                    }.tag(1)
                HomeChatView()
                    .onAppear {
                        backgroundManager.setBackground(nil)
                    }
                    .tabItem {
                        Image(.mainIconChat)
                            .resizable()
                            .renderingMode(.template)
                            .scaledToFit()
                            .foregroundColor(themeManager.themeColor)
                            .frame(width: 10, height: 10)
                        Text("心灵")
                    }.tag(2)
                StatisticsView()
                    .onAppear {
                        backgroundManager.setBackground(nil)
                    }
                    .tabItem {
                        Image(.mainIconStatistics)
                            .resizable()
                            .renderingMode(.template)
                            .scaledToFit()
                            .foregroundColor(themeManager.themeColor)
                            .frame(width: 10, height: 10)
                        Text("统计")
                    }.tag(3)
                MyView()
                    .onAppear {
                        backgroundManager.setBackground(nil)
                    }
                    .tabItem {
                        Image(.mainIconMy)
                            .resizable()
                            .renderingMode(.template)
                            .scaledToFit()
                            .foregroundColor(themeManager.themeColor)
                            .frame(width: 10, height: 10)
                        Text("我的")
                    }.tag(4)
            }
            .navigationDestination(for: Jump.self) { value in
                jumpView(data: value)
            }.accentColor(themeManager.themeColor)
                .onAppear {
                    //请求通知权限
                    LocalNotificationManager.shared.requestAuthorization()
                    if !mainViewModel.isInit {
                        mainViewModel.getUpdate()
                        mainViewModel.isInit = true
                        showNotice()
                        AiCustomUtils.shared.doCustomAi()
                    }
                }
        }.showVoice(isPresent: $commonManager.isVoiceCall, bean: commonManager.callParam)
            .showVoiceWait(isPresent: $commonManager.isVoiceWait, bean: commonManager.callParam)
            .showWeeklyLetter(isPresent: $commonManager.isWeeklyLetter, bean: commonManager.weeklyLetterRole)
            .showWeeklyLetterOpen(isPresent: $commonManager.isWeeklyLetterOpen, isPlaying: $commonManager.isWeeklyLetterOpenAuto, bean: commonManager.weeklyLetterBean)
            .toastView()
        
    }
    
    func showNotice() {
        let noticeMainCache = configManager.config?.getSwitchConfig().noticeMainCache
        let notice = configManager.config?.notice
        if noticeMainCache == nil || noticeMainCache?.isEmpty == true  || notice == nil || notice?.isEmpty == true {
            //没有公告
            return
        }
        if noticeMainCache == AppStorageManager.shared.noticeCache {
            //公告已经加载过了
            return
        }
        AppStorageManager.shared.noticeCache = noticeMainCache ?? ""
        showTipsDialog(title: "公告", text: notice ?? "") {
        }
    }
}

//#Preview {
//    Main()
//}
//定义预览提供者
struct Main_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: Main())
    }
}
